Repository navigation
[lake] Keep a bucket's lake splits in write order for union read - #5288
jaimelopez89 wants to merge 2 commits into
Conversation
Iceberg's planFiles() returns data files in no defined order, and the Flink source read a bucket's lake splits in that order. Sort the planned splits by data sequence number, which follows the tiering commit order. Flink also restores a reader's splits in hash order, which reorders a bucket's pending lake splits for both Iceberg and Paimon. Put lake snapshot splits back in planning order in FlinkSourceReader#addSplits. Document the read order of union read, including that Iceberg order is best effort once the table is compacted. Found by Dustin Nest (@dgnester). Diagnosed and implemented with Claude Code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Where this was tested All runs were local, on macOS with OpenJDK 11.0.29 and Maven 3.9.11, on this branch rebased onto current main. They used the repo's default dependency versions:
Full mvn verify, all passing after the rebase:
The 30 integration tests include both FlinkUnionReadOrderITCase tests. Red/green checks:
Flink 2.x, checked before the rebase:
Not tested locally:
I'm relying on CI for these. |
| // with more than one manifest, planned concurrently on a worker pool. The Flink source | ||
| // reads a bucket's lake splits one after another, so put them back in write order. Each | ||
| // tiering round is one Iceberg commit, so the data sequence number follows write order. | ||
| // The sort is stable, which keeps the files of one commit in the order Iceberg appended |
There was a problem hiding this comment.
Could we qualify the same-commit ordering claim here? Iceberg can roll one commit's files into multiple manifests, while planFiles() does not guarantee manifest order. Files with the same data sequence number remain tied after this sort, so a stable sort may still return them out of write order. Could we test a commit spanning multiple manifests, or describe this case as best effort?
There was a problem hiding this comment.
Thanks @Jackeyzhe, good catch. You're right: my previous comment overclaimed. A commit's files can span several manifests, and planFiles() reads those in parallel, so files tied on data sequence number can come back in any order. In my earlier PR, a stable sort couldn't recover an order that the input never had.
I'm also proposing some fixes in 3aff801:
- Tie-break on file name. A tiering round writes each bucket with a single rolling writer. Iceberg's
OutputFileFactorynames those files<partitionId>-<taskId>-<operationId>-<counter>, with a zero-padded counter that grows as the writer rolls, so the file name restores write order regardless of manifest layout. Only the name is compared, not the full location, so a hashing location provider doesn't affect it. - Legacy tables (with
__offset) are now ordered by the lower bound of__offset, as you suggested in the other thread. That order is exact per bucket, including after compaction, sinceIcebergRewriteDataFilespacks runs of files already sorted by__offset. The planner requests__offsetstats for this, and falls back to sequence-number order if any file lacks them. - Tests:
IcebergSplitPlannerTestnow shuffles the planned splits to stand in for arbitraryplanFiles()order, and checks both one commit of rolled files and a legacy table whose files come from separate writers. Both tests fail without the fix. The existing same-commit case also uses one rolling writer now, which matches what tiering actually does.
| For **log tables** with a bucket key, Flink Union Read returns each bucket's lake data before its Fluss log data, and the hand-off between them has no gap and no overlap. Rows from different buckets are not ordered relative to each other. Whether rows inside the lake data stay in write order depends on the lake format: | ||
|
|
||
| - **Paimon**: the lake data of a bucket is read in write order. | ||
| - **Iceberg**: write order is best effort. A bucket's data files are read in commit order, which is write order until the table is compacted. Compaction rewrites files without keeping row order, so rows rewritten by a single compaction run can come out of order. Data compacted in earlier runs is read before data compacted in later runs, and data written since the last compaction is read in write order. |
There was a problem hiding this comment.
I think the compaction limitation is broader than disorder within a single rewrite. IcebergLakeCommitter commits newly written files before committing the rewrite; rewritten older files can therefore receive a later data sequence number and be read after newer rows. Could we clarify this in the docs, or use __offset ordering for legacy tables as discussed in #5077?
There was a problem hiding this comment.
Thanks again @Jackeyzhe, this one is a real bug in my proposed solution IMO and you described really well. commitRewrite didn't (then) set a data sequence number, so the rewrite, which is committed after the round's appends, gave compacted older rows a newer sequence number than the rows appended in that same round. These rows were read after them, so it contradicted the doc as written.
Took the liberty to propose some fixes, based on your diagnosis, in 3aff801:
- Committer:
IcebergLakeCommitternow commits the rewrite withrewriteFiles.dataSequenceNumber(...)set to the sequence number of the snapshot the rewrite was planned from, the same thing Spark's rewrite does by default (use-starting-sequence-number). Compacted rows now sort before anything tiered after the compaction was planned. Equality deletes committed since then still apply to the rewritten files, because they carry a higher sequence number. - Legacy tables: these are ordered by
__offset(see the other thread), as you suggested, so read order on them is exact even after compaction. - Docs:
union-read.mdnow says that legacy tables keep write order after compaction. For clean tables it says that rows rewritten by a compaction run can be out of order among themselves and relative to files the run didn't rewrite, and that compacted data is still read before later-tiered data. Making compaction itself preserve order on clean tables is a separate change, and I'd rather track it as a follow-up. - Test:
IcebergLakeCommitterTest#testRewriteKeepsSequenceNumberOfStartingSnapshotcommits a round with new data plus a rewrite, and checks that the rewritten files keep the starting snapshot's sequence number, below the new data's. It fails without the fix.
…d compaction Address review feedback on the Iceberg read order: - Files of one commit share a data sequence number, and planFiles() may list them in any order once a commit spans several manifests. Break the tie on the file name: a tiering round writes each bucket with a single rolling writer, whose file names differ only in a zero-padded counter. - Legacy tables (created before FIP-27) carry __offset. Order their files by the lower bound of __offset, which is exact per bucket, also after compaction. - IcebergLakeCommitter committed a rewrite after the round's data, so compacted older rows got a newer sequence number and were read after newer rows. Commit the rewrite with the sequence number of the snapshot it was planned from, as Spark's rewrite does. Update the union read docs to match. Reported by @Jackeyzhe in review. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Created with: Claude Code (Claude Opus 5.5) following your repo guidelines
Purpose
Linked issue: close #5077
A Flink union read of a log table tiered to Iceberg returns a bucket's lake data in arbitrary order. Iceberg's
planFiles()has no defined order (manifests are kept in a hash set and planned in parallel), and Fluss passed that order straight to the reader. Separately, Flink restores a reader's splits in hash order (SourceReaderBasekeeps them in aHashMap), so a bucket's pending lake splits can be reordered after a restore. That affects Paimon as well as Iceberg.Brief change log
IcebergSplitPlanner#plan: sort splits by the data file's data sequence number with a stable sort. Each tiering round is one Iceberg commit, so this is write order. Files of one commit share a sequence number and keep the order Iceberg appended them in. No extra I/O.FlinkSourceReader#addSplits: putLakeSnapshotSplits back in planning order (partition, then split index) before handing them to the split reader. Restored state and enumerator assignments both arrive throughaddSplitsin one batch, so this covers restore and reassignment after a failure. Other split types keep their positions.streaming-lakehouse/union-read.md. It states that for Iceberg, per-bucket order in the lake part is best effort, and describes the effect of compaction, tables without a bucket key, rescaling during the lake phase, and the watermark caveat for event-time jobs.Known limits, as discussed in the issue: compaction rewrites files without keeping row order, so rows rewritten by a single compaction run can still come out of order. Order across buckets is not addressed. Event-time-aware split ordering and order-preserving compaction are left as follow-ups.
Tests
Unit tests:
IcebergSplitPlannerTest#testLogTablePlanKeepsWriteOrderWithinBucket(partitioned and non-partitioned): five interleaved commits into two buckets, then one commit with three files into one bucket. It plans 20 times and asserts that each bucket's splits come out in write order.FlinkSourceReaderTest#testLakeSnapshotSplitsKeepPlanningOrderAcrossRestore: one reader with two buckets of three lake splits each. It reads part of them, checkpoints, reverses the checkpointed splits and restores while lake splits are still pending, then asserts that each bucket's records come out in planning order.FlinkSourceReaderTest: three smaller tests of the sort. They check ordering by partition then split index, that other split types keep their positions, and the case with fewer than two lake splits.LakeSplitGeneratorTest#testLogTableLakeSplitIndexFollowsPlanningOrderWithinBucket: checks that the split index follows planning order within a bucket, which is what the reader sort relies on.Integration tests,
FlinkUnionReadOrderITCase, using a real Fluss cluster and tiering service, a 3-bucket table, five tiering rounds and then log-only data:testBatchUnionReadKeepsWriteOrderWithinBucket: batch union read, asserting each key comes back in write order across the lake and log parts.testStreamingUnionReadKeepsWriteOrderAcrossRestoreDuringLakePhase: a streaming union read that fails once, right after a completed checkpoint, while lake splits are still pending. It asserts order before and after the restore, and that the job actually resumed in the middle of the lake data. Test hooks, not fixed sleeps, coordinate the failure.Red/green: the new planner, reader and integration tests fail without their fixes and pass with them. Without the planner sort, the batch IT read one key as
10-19, 0-9, 30-39, 20-29, 40-49, 50-59.Locally,
mvn verifypasses forfluss-flink-common,fluss-lake-iceberg-commonandfluss-lake-paimon, and spotless and checkstyle pass. Before rebasing, the integration tests also passed against the Flink 2.2 and 2.3 connectors.fluss-lake-iceberg-1.12(JDK 17) was not built locally.API and Format
No API or storage format change. No change to checkpointed state.
Documentation
Adds a "Read Order" section to the union read page.
Found by @dgnester while testing union read on Fluss 1.0. The diagnosis and this change were made with AI tooling (Claude Code).